package com.uphone.kingmall.activity.order;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.allen.library.SuperTextView;
import com.blankj.utilcode.util.LogUtils;
import com.china.zhuoyue.R;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.adapter.RvRefundAdapter;
import com.uphone.kingmall.adapter.RvUpLoadImageAdapter;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.OrderBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.main.MainActivity;
import com.uphone.kingmall.utils.ActivityCollector;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.PictureSelectUtils;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.utils.intent.IntentData;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 孙增艳
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2019/2/26
 *     desc   : 申请退款
 *     version: 1.0
 * </pre>
 */
public class ApplyForRefundActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_goods)
    RecyclerView rvGoods;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_wuliu)
    TextView tvWuliu;
    @BindView(R.id.et_infomation)
    EditText etInfomation;
    @BindView(R.id.rv_upload_image)
    RecyclerView rvUploadImage;
    @BindView(R.id.iv_upload)
    ImageView ivUpload;
    @BindView(R.id.ll_upload_image)
    LinearLayout llUploadImage;
    @BindView(R.id.stv_submit)
    SuperTextView stvSubmit;
    @BindView(R.id.ll_wuliu)
    LinearLayout llWuliu;

    @IntentData
    private int type = -1;
    @IntentData
    private OrderBean.DataBean bean;

    private List<LocalMedia> localMediaList = new ArrayList<>();
    private RvUpLoadImageAdapter upLoadImageAdapter;
    private String company;
    private String number;
    private String comNo;
    private RvRefundAdapter refundAdapter;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_apply_for_refund;
    }

    @Override
    public void initView() {
        setStatusBar();
        switch (type) {
            case 1://未发货时只退款
                llUploadImage.setVisibility(View.GONE);
                tvTitle.setText("退款");
                llWuliu.setVisibility(View.GONE);
                break;
            case 2://已发货后退款退货
                llUploadImage.setVisibility(View.VISIBLE);
                llWuliu.setVisibility(View.VISIBLE);
                tvTitle.setText("退货退款");
                break;
            default:
                break;
        }

        refundAdapter = new RvRefundAdapter(this, bean.getGoodslist());
        rvGoods.setLayoutManager(new LinearLayoutManager(this));
        rvGoods.setAdapter(refundAdapter);

        upLoadImageAdapter = new RvUpLoadImageAdapter(this, localMediaList);
        rvUploadImage.setLayoutManager(new GridLayoutManager(this, 3));
        rvUploadImage.setAdapter(upLoadImageAdapter);

        tvPrice.setText(bean.getOrderFee() + "");

    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.iv_back, R.id.iv_upload, R.id.stv_submit, R.id.ll_wuliu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_wuliu:
                CommonUtil.startIntent(this, ApplyForRefund2Activity.class);
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_upload://上传图片
                PictureSelectUtils.openMultiplePicture(ApplyForRefundActivity.this, localMediaList
                        , 6, false, 0, 0, PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.stv_submit://提交
                switch (type) {
                    case 1://只退款
                        submit();
                        break;
                    case 2://退货退款，去填写快递公司及运单号码
                        submit();
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }

    private void submit() {
        HttpParams params = new HttpParams();
        if (type == 2) {
            if (TextUtils.isEmpty(number) || TextUtils.isEmpty(company) || TextUtils.isEmpty(comNo)) {
                ToastUtils.showShortToast(this, "请填写物流信息");
                return;
            }
            params.put("sendCoNm", company);
            params.put("sendCoNo", comNo);
            params.put("sendNo", number);
            List<File> files = new ArrayList<>();
            for (int i = 0; i < localMediaList.size(); i++) {
                files.add(new File(localMediaList.get(i).getPath()));
            }
            params.putFileParams("imgs", files);
        }
        params.put("orderId", bean.getOrderId());
        params.put("refundInfo", etInfomation.getText().toString());
        OkGoUtils.progressRequest(MyUrl.applyRefund, this, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    ToastUtils.showShortToast(ApplyForRefundActivity.this, "提交成功");
                    // 退款详情页
                    CommonUtil.startIntent(ApplyForRefundActivity.this, OrderRefundDetailsActivity.class, bean.getOrderId());

                    ActivityCollector.removeAll(MainActivity.class,OrderRefundDetailsActivity.class);
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.e("咋回事");
        if (resultCode == 10) {
            // 物流信息 ApplyForRefund2Activity
            LogUtils.e("咋回事");
            if (data != null) {
                company = data.getStringExtra("company");
                number = data.getStringExtra("number");
                comNo = data.getStringExtra("comNo");
                LogUtils.e("咋回事  " + company + "  " + number + "   " + comNo);
                if (!TextUtils.isEmpty(company) && !TextUtils.isEmpty(number) && !TextUtils.isEmpty(comNo)) {
                    tvWuliu.setText(number + "");
                }
            }
        } else if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    localMediaList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    upLoadImageAdapter.setNewData(localMediaList);
                    break;
                default:
                    break;
            }
        }
    }
}
